package com.bjsxt.service.impl;

import com.bjsxt.mapper.RoleMapper;
import com.bjsxt.mapper.RoleMenuMapper;
import com.bjsxt.pojo.PageA;
import com.bjsxt.pojo.Role;
import com.bjsxt.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public PageA<Role> selectRoleSice(Integer page, Integer rows) {
        Page pg = PageHelper.startPage(page, rows);
        List<Role> list = roleMapper.selectRole();
        PageA<Role> pgg=new PageA<>(list,pg.getTotal());

        return pgg;
    }

    @Override
    public Integer insertRoleSice(Role role,Integer[] mids) {

        Integer integer = 0;
        try {
            integer = roleMapper.insertRole(role);
            for (Integer mid:mids){
                roleMenuMapper.insertRoleMenu(role.getRid(),mid);
            }
            integer=1;
        } catch (Exception e) {
            throw e;
        }

        return integer;
    }

    @Override
    public List<Integer> selectRoleMenuSice(Integer rid) {

        return roleMenuMapper.selectRoleMenu(rid);
    }

    @Override
    public Integer deleteRole(Role role, Integer[] mids) {
        int a=0;
        try {
            roleMapper.updateRole(role);

            roleMenuMapper.deleteRoleMenu(role.getRid());

            for (Integer mid:mids){
                roleMenuMapper.insertRoleMenu(role.getRid(),mid);
            }
            a=1;
        } catch (Exception e) {
            throw e;
        }

        return a;
    }

    @Override
    public Integer deleteRoleOne(Integer rid) {
        return roleMapper.deleteRole(rid);
    }

    //添加管理员查询
    @Override
    public List<Role> selectRoleNameSice() {
        return roleMapper.selectRoleName();
    }
}
